<?php

namespace App\Imports;

use App\Models\Assess\OrderAndCustomersOrderDetailModel;
use App\Models\Assess\OrderAndCustomersOrderExtendModel;
use App\Models\Assess\OrderAndCustomersOrderModel;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\ToCollection;

class OrderImport implements ToCollection
{

    public $message=[];

    /**
     * @param Collection $collection
     */
    public function collection(Collection $collection)
    {
        // 循环遍历数据
        $OrderAndCustomersOrderModel = new OrderAndCustomersOrderModel();
        $OrderAndCustomersOrderExtendModel = new OrderAndCustomersOrderExtendModel();
        $OrderAndCustomersOrderDetailModel = new OrderAndCustomersOrderDetailModel();
        foreach ($collection as $row) {
            try {
                $item = $row->toArray();
                if (empty($item[0])) continue;
                if (!empty($item) && $item[0] == 'amazon-order-id') continue;

                $orderData = [
                    'order_id' => $item[0],
                    'platform'=>'amazon',
                    'platform_paid_date' => $this->excelTime($item[1],true),
                    'user_account' => $item[5] ?: '',
                    'country_code'=>$item[11] ?: '',
                    'create_time'=>time()
                ];
                $orderExists = $OrderAndCustomersOrderModel->where([
                    ['order_id', '=', $orderData['order_id']],
                    ['platform', '=', $orderData['platform']],
                    ['user_account', '=', $orderData['user_account']]
                ])->first('id');

                if (!empty($orderExists['id'])){
                    Log::channel('orderLog')->debug($orderData);
                    Log::channel('orderLog')->debug('当前记录已存在订单表中');
                    $this->message[] = '订单id:'.$orderData['order_id'].' 当前记录已存在订单列表中';
                    continue;
                }
                $id = $OrderAndCustomersOrderModel->insertGetId($orderData);

                $extendData = [
                    'order_id'=>$id,
                    'city_name'=>$item[8] ?: '',
                    'state'=>$item[9] ?: '',
                    'postal_code'=>$item[10] ?: ''
                ];
                $OrderAndCustomersOrderExtendModel->insert($extendData);
                $itemData = [
                    'order_id'=>$id,
                    'platform_sku' => $item[3] ?: '',
                    'product_title' => $item[2] ?: '',
                    'asin' => $item[4] ?: '',
                    'item_price'=>$item[6] ?: 0.00
                ];
                $OrderAndCustomersOrderDetailModel->insert($itemData);
            }catch (\Exception $e){
                $logInfo = $e->getMessage().' '.$e->getFile().' '.$e->getLine();
                $this->message[] = $logInfo;
                Log::channel('orderLog')->debug($logInfo);
            }
        }
    }

    function excelTime($days, $time=false){
        if(is_numeric($days)){
            //based on 1900-1-1
            $jd = GregorianToJD(1, 1, 1970);
            $gregorian = JDToGregorian($jd+intval($days)-25569);
            $myDate = explode('/',$gregorian);
            $myDateStr = str_pad($myDate[2],4,'0', STR_PAD_LEFT)
                ."-".str_pad($myDate[0],2,'0', STR_PAD_LEFT)
                ."-".str_pad($myDate[1],2,'0', STR_PAD_LEFT)
                .($time?" 00:00:00":'');
            return $myDateStr;
        }
        return $days;
    }

}


